package mymall.custom;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;

public class data {

    String data = "";

    public data() {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = factory.newDocumentBuilder();

            Document document = builder.parse("src/mymall/custom/data.xml");

            Element rootElement = document.getDocumentElement();

            NodeList rootChildren = rootElement.getChildNodes();

           
            for (int i = 0; i < rootChildren.getLength(); i++) {

                Node node = rootChildren.item(i);

                if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("offer")) {
                    
                    Element offer = (Element) node;

                    String title = offer.getAttribute("title");
                    String descr = offer.getAttribute("descr");
                    String from = offer.getAttribute("from");
                    String to = offer.getAttribute("to");
                   
                    data = data+title+"\t"+descr+"\t"+from+"\t"+to+"\n";

                }

            }
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }

    public String getData(){

        return data;

    }

    public void add(String titolo, String descr, String da, String a ){

        /*
        String offerta = "<offer title=\""+titolo+"\" descr=\""+descr+"\" from=\""+da+"\" to=\""+a+"\" >";

        try{

            FileReader fr = new FileReader("src/mymall/custom/data.xml");

            BufferedReader inFile = new BufferedReader (fr);

            inFile.readLine(); //Ti legge una riga del file di testo. Quindi se hai più righe inserisci questa porzione di codice in un for o in un while.

            inFile.close();

        }
        catch (FileNotFoundException exception){}
        catch (IOException exception){}
         * 
         */
    }


}